
***********************************************
* Rwanda provider study
***********************************************

***********************************************
* Contents
***********************************************

	* (0.0) Set up
	* (0.1) Grab variables
	* (0.2) Define indicators
	
***********************************************
* (0.0) Set up
***********************************************
	
	clear all
	clear matrix
	set mem 250m
	set matsize 1000
	set more off
	tempfile temp1
	* set scheme lean1, permanently
	pause on
    eststo clear
	

	cd "C:\Users\Diana\Dropbox\RWA_P4P\SPA\do_files"
	* Define file globals
	do rwanda_global_define.do

    cap log close
    log using `"$d_out\logs\logresults_`c(current_date)'.txt"', text replace
	
***********************************************
* (0.1) Grab basic variables
***********************************************
	 
	use "$f_inventory", clear

	gen q_meet = (q118==1) if !mi(q118)
	replace q_meet=0 if q117==2
	global q_meet_desc "Monthly management meetings (x100)"

	gen q_cmeet = (q122==1) if !mi(q122)
	replace q_cmeet=0 if q121==2
	global q_cmeet_desc "Monthly community meetings (x100)"

	gen q_opin = ( q124!="Y" & q124!="Z" ) if q124!=""
	global q_opin_desc "System for client opinion (x100)"

	gen q_ochange = ( q126!="Y" & q124!="Z" ) if q126!=""
	global q_ochange_desc "Client opinion led to changes in past 3 months (x100)"

	gen q_qual = ( q127==1) if !mi(q127)
	global q_qual_desc "Routine quality assurance (x100)"

	gen q_accred = ( q131!=3) if !mi(q131)
	global q_accred_desc "Outside accreditation program (x100)"
	 
	gen q_icontr = ( q132!=3) if !mi(q132)
	global q_icontr_desc "Infection control committee (x100)"

	gen q_supvis = ( q136==1) if !mi(q136)
	global q_supvis_desc "Supervisor visit in past 6 months (x100)"
	
	recode q137_* (2=0) (8=.)
	egen q_supact=rowtotal(q137_1 q137_2 q137_3 q137_4 q137_5 q137_6 q137_7 q137_8), m
	recode q_supact (0/5=0) (6/8=1)
	global q_supact_desc "Supervisor checks at least 6 of 8 items (x100)"
	
	gen q_rmaint = inlist(q138, 1, 2, 3) if !mi(q138)
	global q_rmaint_desc "Routine infrastructure maintenance/repair (x100)"

	gen q_pmaint = inlist(q139, 1, 2, 3) if !mi(q139)
	global q_pmaint_desc "Routine preventing maintenance (x100)"
	
	recode q_* (1=100)
	keep facil q_* 
	
	local manage_var "q_meet q_cmeet q_opin q_ochange q_qual q_accred"
	local manage_var "`manage_var' q_icontr q_supvis q_supact q_rmaint q_pmaint"
	
	foreach var in `manage_var' {
		lab var `var' "$`var'_desc"
	}
	
	save `temp1', replace

***********************************************
* Cleanliness
***********************************************

	use "$f_inventory", clear

	local immun_code=248
	local sc_code=280
	local fp_code=343
	local anc_code=452
	local del_code=590
	local sti_code=630
	
	foreach var in immun sc fp anc del sti {
		* Cleanliness of different areas
		recode q``var'_code'_* (2=0)
		recode q``var'_code'_3 (1=0) (0=1) /* Broken equipment, positive report is bad */
		
		local needle=``var'_code'+1
		gen `var'_needle=q`needle'==2 if !mi(q`needle')
		tab q`needle'
		tab `var'_needle
		
		local sharps=``var'_code'+2
		gen `var'_sharps=q`sharps'==2 if !mi(q`sharps')
		tab q`sharps'
		tab `var'_sharps
		
		local bandage=``var'_code'+3
		gen `var'_bandage=q`bandage'==3 if !mi(q`bandage')
		tab q`bandage'
		tab `var'_bandage
		
		egen c_`var'=rowtotal(q``var'_code'_* `var'_needle `var'_sharps `var'_bandage), m
	
		recode c_`var' (0/8=0) (9/10=1)
	}
	
	recode c_* (1=100)
	
	global c_immun_desc "Child immunization area clean (at least 9/10 items) (x100)"
	global c_sc_desc "Child curative services area clean (at least 9/10 items) (x100)"
	global c_fp_desc "Family planning area clean (at least 9/10 items) (x100)"
	global c_anc_desc "ANC area clean (at least 9/10 items) (x100)"
	global c_del_desc "Delivery area clean (at least 9/10 items) (x100)"
	global c_sti_desc "STI service area clean (at least 9/10 items) (x100)"

	local clean_var "c_immun c_sc c_fp c_anc c_del c_sti"
	
	foreach var in `clean_var' {
		lab var `var' "$`var'_desc"
	}

	keep facil c_*
	merge 1:1 facil using `temp1', nogen
	save "$d_interm\manage", replace
	
***********************************************
* (0.2) Combine into indicators 
* 		(includes various methods of weighting)
***********************************************

	use "$f_inventory", clear
	
	gen m_mmin=inlist(q119,1,2) if !mi(q119)
	replace m_mmin=0 if q117==2 /* no meetings */
	replace m_mmin=0 if q118!=1 /* meetings infrequent */
	gen m_mmin_w = 1
	global m_mmin_desc "Minutes for monthly management meetings"

	gen m_cmin=inlist(q123,1,2) if !mi(q123)
	replace m_cmin=0 if q121==2 /* no meetings */
	replace m_cmin=0 if q122!=1 /* meetings infrequent */
	gen m_cmin_w = 1
	global m_cmin_desc "Minutes for monthly community meetings"
	
	gen m_qmin=inlist(q129_5,1,2) if !mi(q129_5)
	replace m_qmin=0 if q127==2 /* no routine quality assurance */
	gen m_qmin_w = 1
	global m_qmin_desc "Quality assurance reports"
	
	recode q139 (8=.)
	gen m_equip=inlist(q139,1,2,3) if !mi(q139)
	gen m_equip_w = 1
	global m_equip_desc "Routine equipment maintenance"
	
	gen m_avail=inlist(q104,1,2) if !mi(q104)
	gen temp_avail=inlist(q105,1,2) if !mi(q105)
	replace m_avail=0.5*temp_avail if m_avail==0
	gen m_avail_w = 1
	global m_avail_desc "Services available full time"
	
	gen m_wait_cimmun=q236==1 if !mi(q236)
	gen m_wait_sc=q258==1 if !mi(q258)
	gen m_wait_fp=q306==1 if !mi(q306)
	gen m_wait_anc=q404==1 if !mi(q404)
	
	keep facil m_*
	
	save "$d_interm\manage_mult", replace
	
	**** Note, this is for HIV/AIDS only
	use "$f_hmis", clear
	gen m_hmis=q1417
	recode m_hmis (3/4=0) (2=0.5)
	gen m_hmis_w = 1
	global m_hmis_desc "Medical systems reports observed"
	
	keep facil m_*
	merge 1:1 facil using "$d_interm\manage_mult", nogen
	save "$d_interm\manage_mult", replace
	
	
	use "$f_outp", clear
	gen m_wait_hiv=q1260==1 if !mi(q1260)
	collapse m_wait_hiv, by(facil)
	
	merge 1:1 facil using "$d_interm\manage_mult", nogen
	
	gen m_wait=0
	gen m_wait_clinic=0
	
	foreach var in cimmun sc fp anc hiv {
		replace m_wait=m_wait+m_wait_`var' if !mi(m_wait_`var')
		replace m_wait_clinic=m_wait_clinic+1 if !mi(m_wait_`var')
	}
	
	drop m_wait_cimmun m_wait_sc m_wait_fp m_wait_anc m_wait_hiv
	replace m_wait=m_wait/m_wait_clinic
	gen m_wait_w = 1
	global m_wait_desc "Covered waiting areas"
	
	gen manage_mult=0
	gen manage_weight=0
	
	foreach var in m_mmin m_cmin m_hmis m_qmin m_equip m_avail  {
		replace `var'_w = . if `var'==.
		replace manage_mult=manage_mult+`var'*`var'_w if !mi(`var') & !mi(`var'_w)
		replace manage_weight=manage_weight+`var'_w if !mi(`var'_w)
	}
	
	replace manage_mult=manage_mult/manage_weight
	/// All observations have at least 2/3 of items
	* replace manage_mult=. if anc_weight<17
	
	global manage_mult_desc "Management indicator$^\dag$"
	
	merge 1:1 facil using "$r_fac_controls", nogen
	pca m_mmin m_cmin m_hmis m_qmin m_equip m_avail if treatment==0
	predict manage_mult_pca
	global manage_mult_pca_desc "Management indicator$^*$"
	
	foreach var in m_mmin m_cmin m_hmis m_qmin m_equip /*
		*/ m_avail m_wait manage_mult manage_mult_pca {
		lab var `var' "$`var'_desc"
	}
	
	save "$d_interm\manage_mult", replace
	
***********************************************
* Hygiene indicator
***********************************************

	use "$f_inventory", clear

	local immun_code1=241
	local immun_code2=248
	local sc_code1=268
	local sc_code2=280
	local fp_code1=327
	local fp_code2=343
	local anc_code1=438
	local anc_code2=452
	local del_code1=536
	local del_code2=590
	local sti_code1=628
	local sti_code2=630
	
	foreach var in soap disin clean org wat {
		gen cl_`var'=0
		gen cl_`var'_clinic=0
	}
	
	foreach var in immun sc fp anc del sti {
		/// Handwashing soap
		gen cl_soap_`var'=inlist(q``var'_code1'_04,1,2) if !mi(q``var'_code1'_04)
		replace cl_soap=cl_soap+cl_soap_`var' if !mi(cl_soap_`var')
		replace cl_soap_clinic=cl_soap_clinic+1 if !mi(cl_soap_`var')	
		
		/// Disinfectant
		gen cl_disin_`var'=inlist(q``var'_code1'_10,1,2) if !mi(q``var'_code1'_10)
		gen temp_disin_`var'=inlist(q``var'_code1'_11,1,2) if !mi(q``var'_code1'_11)
		replace cl_disin_`var'=1 if temp_disin_`var'==1
		replace cl_disin=cl_disin+cl_disin_`var' if !mi(cl_disin_`var')
		replace cl_disin_clinic=cl_disin_clinic+1 if !mi(cl_disin_`var')	

		/// Cleanliness
		* Trash bin 
		gen cl_bin_`var'=inlist(q``var'_code1'_06,1,2) if !mi(q``var'_code1'_06)
		
		* No loose trash (must have all items)
		gen cl_trash1_`var'=inlist(q``var'_code2'_1,1) if !mi(q``var'_code2'_1) /* Floors swept */
		gen cl_trash1m_`var'=q``var'_code2'_1!=.
		gen cl_trash2_`var'=inlist(q``var'_code2'_2,1) if !mi(q``var'_code2'_2) /* Counters swept */
		gen cl_trash2m_`var'=q``var'_code2'_2!=.
		* Don't want positive response to 3
		gen cl_trash3_`var'=inlist(q``var'_code2'_3,2) if !mi(q``var'_code2'_3) /* No broken equipment */
		gen cl_trash3m_`var'=q``var'_code2'_3!=.
		
		egen cl_trash_`var'=rowtotal(cl_trash1_`var' cl_trash2_`var' cl_trash3_`var'), m
		egen cl_trashm_`var'=rowtotal(cl_trash1m_`var' cl_trash2m_`var' cl_trash3m_`var'), m
	
		replace cl_trash_`var'=cl_trash_`var'/cl_trashm_`var'
		recode cl_trash_`var' (0/.999=0)
		
		* Sharps container
		gen cl_sbin_`var'=inlist(q``var'_code1'_07,1,2) if !mi(q``var'_code1'_07)
		
		replace cl_clean=cl_clean+cl_bin_`var' if !mi(cl_bin_`var')
		replace cl_clean_clinic=cl_clean_clinic+1 if !mi(cl_bin_`var')
		
		replace cl_clean=cl_clean+cl_trash_`var' if !mi(cl_trash_`var')
		replace cl_clean_clinic=cl_clean_clinic+1 if !mi(cl_trash_`var')
		
		replace cl_clean=cl_clean+cl_sbin_`var' if !mi(cl_sbin_`var')
		replace cl_clean_clinic=cl_clean_clinic+1 if !mi(cl_sbin_`var')
		
		/// Organic waste
		local `var'_sharps = ``var'_code2'+1
		di "``var'_sharps'"
		local `var'_overfl = ``var'_code2'+2
		di "``var'_overfl'"
		local `var'_band = ``var'_code2'+3
		di "``var'_band'"

		gen cl_org1_`var'=inlist(q``var'_sharps',2) if !mi(q``var'_sharps')
		gen cl_org1m_`var'=q``var'_sharps'!=.
		gen cl_org2_`var'=inlist(q``var'_overfl',2) if !mi(q``var'_overfl')
		gen cl_org2m_`var'=q``var'_overfl'!=.
		gen cl_org3_`var'=inlist(q``var'_band',3) if !mi(q``var'_band')
		gen cl_org3m_`var'=q``var'_band'!=.
		
		egen cl_org_`var'=rowtotal(cl_org1_`var' cl_org2_`var' cl_org3_`var'), m
		egen cl_orgm_`var'=rowtotal(cl_org1m_`var' cl_org2m_`var' cl_org3m_`var'), m
		
		replace cl_org_`var'=cl_org_`var'/cl_orgm_`var'
		recode cl_org_`var' (0/.999=0)
		replace cl_org=cl_org+cl_org_`var' if !mi(cl_org_`var')
		replace cl_org_clinic=cl_org_clinic+1 if !mi(cl_org_`var')
		
		/// Water dispensers
		gen cl_wat_`var'=inlist(q``var'_code1'_01,1,2) if !mi(q``var'_code1'_01)
		replace cl_wat_`var'=1 if inlist(q``var'_code1'_02,1,2)
		replace cl_wat=cl_wat+cl_wat_`var' if !mi(cl_wat_`var')
		replace cl_wat_clinic=cl_wat_clinic+1 if !mi(cl_wat_`var')
	}
	
	/// Aggregate across clinics
	foreach var in soap disin clean org wat {
		replace cl_`var'=cl_`var'/cl_`var'_clinic
	}

	* Organic waste strongly penalized if exists anywhere
	recode cl_org (0/.999=-5)
	
	/// Availability of water source
	gen cl_watsource=inlist(q155,1,2,3,4,5,6,11,12) if !mi(q155)

	/// Functioning incinerator
	gen cl_incin=inlist(q183,2,3) if !mi(q183)
	gen cl_incin_temp=inlist(q183,2,3) if !mi(q185)
	replace cl_incin=1 if cl_incin_temp==1
	
	/// Functioning sterilizing materials
	gen cl_steril=0
	gen cl_sterilm=0
	
	forval i=1/4 {
		gen temp_steril = inlist(b180_`i',1) if !mi(b180_`i')
		replace temp_steril=0 if inlist(a180_`i',3,8)
		replace cl_steril=cl_steril+temp_steril if !mi(temp_steril)
		replace cl_sterilm=cl_sterilm+1 if !mi(temp_steril)
		drop  temp_steril
	}
	
	replace cl_steril=cl_steril/cl_sterilm
	recode cl_steril (.001/max=1)
	
	/// Multiplier weights
	gen cl_soap_w=1
	gen cl_disin_w=1
	gen cl_clean_w=2
	gen cl_org_w=1
	gen cl_incin_w=1
	gen cl_watsource_w=1
	gen cl_wat_w=1
	gen cl_steril_w=1
	
	global cl_soap_desc "Soap availability (averaged across clinics)"
	global cl_disin_desc "Disinfectant availability (averaged across clinics)"
	global cl_clean_desc "Cleanliness of clinics (averaged across clinics)"
	global cl_org_desc "No organic waste (1: fully absent, -5: present somewhere)"
	global cl_incin_desc "Functioning incinerator"
	global cl_watsource_desc "Clean water source available"
	global cl_wat_desc "Water dispensers available (averaged across clinics)"
	global cl_steril_desc "Functioning sterilizing equipment"
	
	local clean_list="cl_bin_immun cl_trash_immun cl_sbin_immun"
	local clean_list="`clean_list' cl_bin_sc cl_trash_sc cl_sbin_sc"
	local clean_list="`clean_list' cl_bin_fp cl_trash_fp cl_sbin_fp"
	local clean_list1="cl_bin_anc cl_trash_anc cl_sbin_anc"
	local clean_list1="`clean_list1' cl_bin_del cl_trash_del cl_sbin_del"
	local clean_list1="`clean_list1' cl_bin_sti cl_trash_sti cl_sbin_sti"
		

	foreach var in bin trash sbin {
		gen `var'_compiled=0
		gen `var'_hasdata=0
		foreach clinic in immun sc fp anc del sti {
			replace `var'_hasdata=`var'_hasdata+1 if !mi(cl_`var'_`clinic')
			replace `var'_compiled=`var'_compiled+cl_`var'_`clinic' if !mi(cl_`var'_`clinic')
		}
		gen `var'_avg=`var'_compiled/`var'_hasdata
	}
	
	
	merge 1:1 facil using "$r_fac_controls", nogen
	* pca `clean_list' `clean_list1' if treatment==0
	pca bin_avg trash_avg sbin_avg if treatment==0
	predict cl_clean_pca
	global cl_clean_pca_desc "Cleanliness of clinics$^*$" 
	
	* Trying to figure out which are the most missing
	foreach var in `clean_list' `clean_list1' {
		di "`var'"
		count if `var'==.
	}
	
	gen sc_total=!mi(cl_bin_sc) & !mi(cl_trash_sc) & !mi(cl_sbin_sc)
	egen cl_clean_sc=rmean(cl_bin_sc cl_trash_sc cl_sbin_sc) if sc_total==1
	global cl_clean_sc_desc "Cleanliness of child clinic"
	
	pca cl_bin_sc cl_trash_sc cl_sbin_sc if treatment==0
	predict cl_clean_sc_pca
	global cl_clean_sc_pca_desc "Cleanliness of child clinic, PCA"
	
	keep facil cl_soap cl_soap_w cl_disin cl_disin_w cl_clean cl_clean_w cl_org cl_org_w /*
		*/ cl_incin cl_incin_w cl_watsource cl_watsource_w cl_wat cl_wat_w cl_steril cl_steril_w /*
		*/ cl_clean_pca cl_clean_sc cl_clean_sc_pca
		
	gen clean_mult=0
	gen clean_weight=0
	
	foreach var in cl_soap cl_disin cl_clean cl_org cl_incin cl_watsource cl_wat cl_steril {
		replace `var'_w = . if `var'==.
		replace clean_mult=clean_mult+`var'*`var'_w if !mi(`var') & !mi(`var'_w)
		replace clean_weight=clean_weight+`var'_w if !mi(`var'_w)
	}
	
	replace clean_mult=clean_mult/clean_weight
	/// Drop observations with less than total items < 8
	replace clean_mult=. if clean_weight<8
	
	global clean_mult_desc "Sanitation indicator (P4P multiplier)$^\dag$"

	
	merge 1:1 facil using "$d_interm\manage_mult", nogen
	save "$d_interm\manage_mult", replace
	
	use "$f_inventory", clear
	gen q_inpatbed=q116b
	/* No inpatient care */
	replace q_inpatbed=. if q114==2 
	
	gen q_nightbed=q116a
	/* No overnight or inpatient care */
	replace q_nightbed=. if q115==2 
	
	gen q_elec=q149
	recode q_elec (1/2=1) (3=0)
	replace q_elec=1 if q151==1 | q151==2
	
	global q_elec_desc "Any source of electricity"
	
	keep facil q_*
	merge 1:1 facil using "$r_fac_controls", nogen
	
	foreach var in inpatbed nightbed {
		gen q_`var'pc=q_`var'/catchment*10000
	}	
	
	gen q_bedpc=q_inpatbedpc+q_nightbedpc
	gen q_genbedtarget=q_bedpc/25 if !mi(q_bedpc)
	recode q_genbedtarget (1/max=1)
	
	global q_inpatbedpc_desc "Inpatient beds/10,000 people"
	global q_nightbedpc_desc "Overnight beds/10,000 people"
	global q_bedpc_desc "Beds (inpatient and overnight)/10,000 people"
	global q_genbedtarget_desc "N/25 beds/10,000 people (max 1)"
	merge 1:1 facil using "$d_interm\manage_mult", nogen
	
	gen q_basicsupplies=.5*cl_soap+.5*cl_disin
	global q_basicsupplies_desc "Sanitation supplies indicator$^\dag$"
	
	merge 1:1 facil using "$r_fac_controls", nogen
	pca cl_soap cl_disin if treatment==0
	predict q_supplies_pca
	global q_supplies_pca_desc "Sanitation supplies indicator$^*$"
	
	
	gen q_basicequip=1/6*q_elec+1/6*cl_incin+1/6*cl_watsource+1/6*cl_wat+1/6*cl_steril+1/6*q_genbedtarget
	global q_basicequip_desc "Basic equipment indicator$^\dag$"
	
	pca q_elec cl_incin cl_watsource cl_wat cl_steril q_genbedtarget if treatment==0
	predict q_equip_pca
	global q_equip_pca_desc "Basic equipment indicator$^*$"
	
	foreach var in cl_soap cl_disin cl_clean cl_org cl_incin /*
	*/ cl_watsource cl_wat cl_steril clean_mult q_inpatbed /*
	*/ q_nightbed q_bedpc q_basicsupplies q_elec q_basicequip /*
	*/ cl_clean_pca q_supplies_pca q_equip_pca manage_mult_pca {
		lab var `var' "$`var'_desc"
	}
	

foreach var in cl_clean_pca q_supplies_pca q_equip_pca manage_mult_pca {
	egen max_`var'=max(`var')
	egen min_`var'=min(`var')
	* Normalizing to be fraction of achievement
	replace `var'=(`var'-min_`var')/(max_`var'-min_`var')
}

drop max* min*
save "$d_interm\manage_mult", replace
